package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author LEO
 * @Date 2021年12月10日 10:55
 */
@Api(tags = "商品基础属性接口")
@RestController
@RequestMapping("admin/product")
//@CrossOrigin
public class BaseManageController {
    @Autowired
    private ManageService manageService;


    /**
     * 查询所有的一级分类信息
     * @return
     */
    //http://api.gmall.com/admin/product/getCategory1     <List<BaseCategory1>>
    @GetMapping("getCategory1")
    public Result getCategory1() {
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    /**
     * 根据一级分类Id 查询二级分类数据 <List<BaseCategory2>>
     * @param category1Id
     * @return
     */
    //http://api.gmall.com/admin/product/getCategory2/{category1Id}
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable("category1Id") Long category1Id) {
        List<BaseCategory2> category2List = manageService.getCategory2(category1Id);
        return Result.ok(category2List);
    }


    /**
     * 根据二级分类Id 查询三级分类数据  <List<BaseCategory3>>
     * @param category2Id
     * @return
     */
    // http://api.gmall.com/admin/product/getCategory3/{category2Id}
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable("category2Id") Long category2Id) {
        List<BaseCategory3> category3List = manageService.getCategory3(category2Id);
        return Result.ok(category3List);
    }

    /**
     * 根据分类Id 获取平台属性数据 <List<BaseAttrInfo>>
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable("category1Id") Long category1Id,
                                                   @PathVariable("category2Id") Long category2Id,
                                                   @PathVariable("category3Id") Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = manageService.getBaseAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baseAttrInfoList);
    }


    /**
     * 保存平台属性方法
     * @param baseAttrInfo 这个对象的属性与传递的数据格式类似  JSON-》JavaObject
     * @return
     */
    //http://api.gmall.com/admin/product/saveAttrInfo
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        // 前台数据都被封装到该对象中baseAttrInfo
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }


    //  根据平台属性ID获取平台属性对象数据  <List<BaseAttrValue>>
    //http://api.gmall.com/admin/product/getAttrValueList/{attrId}
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId) {
        // select * from base_attr_value where attr_id =attrId
        //先判断一下当前是否存在该属性，如果有属性，则调用属性对象的属性集合
       BaseAttrInfo baseAttrInfo=  manageService.getBaseAttrInfo(attrId);
       if (baseAttrInfo != null){
           List<BaseAttrValue> baseAttrValueList=   manageService.getAttrValueList(attrId);
           return Result.ok(baseAttrValueList);
       }
        return Result.ok();
    }

}
